package com.abbas.wallerpaper.ui;

import static java.security.AccessController.getContext;

import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.abbas.wallerpaper.base.BaseActivity;
import com.abbas.wallerpaper.fragment.ContainFragment;
import com.abbas.wallerpaper.R;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private AppBarLayout barlayout;
    private Toolbar tool;
    private TextView title;
    private TabLayout tab;
    private ViewPager vp;
    private NavigationView nv;
    private DrawerLayout dl;

    private String[] tabTitles = new String[]{"3D","自然","时尚","动物","美食"};

    private List<Fragment> pages = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init() {
        dl = findViewById(R.id.dl);
        barlayout = (AppBarLayout) findViewById(R.id.barlayout);
        tool = (Toolbar) findViewById(R.id.tool);
        tab = (TabLayout) findViewById(R.id.tab);
        vp = (ViewPager) findViewById(R.id.vp);
        title = findViewById(R.id.title);
        nv = findViewById(R.id.nv);

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, dl, tool, R.string.open, R.string.close);
        actionBarDrawerToggle.syncState();

        tool.setTitle("Wallpaper");

    }

    @Override
    protected void initEvent() {

        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab,true,R.color.yellow_basic,R.color.tb_none);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab,false,R.color.yellow_basic,R.color.tb_none);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return pages.get(position);
            }

            @Override
            public int getCount() {
                return 5;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabTitles[position];
            }
        });

        for (int i = 0; i < 5; i++) {
            TabLayout.Tab tab = this.tab.newTab();
            tab.setText("Hello");
            tab.setCustomView(getTabView());
            this.tab.addTab(tab);
            pages.add(new ContainFragment());
        }
        tab.setupWithViewPager(vp);
    }

    private View getTabView(){
        View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_item, null, false);
        TextView tv = inflate.findViewById(R.id.tag);
        tv.setText("Hello");
        return inflate;
    }

    /**
     * 每次选择更新Item样式
     */
    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect, int textSelectColor, int textUnSelectColor) {
        if (isSelect) {
            //选中加粗
            TextView tabSelect = tab.getCustomView().findViewById(R.id.tag);
//            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
            tabSelect.setTextColor(ContextCompat.getColor(MainActivity.this, textSelectColor));//颜色
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = tab.getCustomView().findViewById(R.id.tag);
//            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setTextColor(ContextCompat.getColor(MainActivity.this, textUnSelectColor));
            tabUnSelect.setText(tab.getText());
        }
    }


}